package com.example.controller;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
public class MQController {
    @Value("${spring.rabbitmq.queue-name}")
    private String queueName;

    @Resource
    private AmqpTemplate amqpTemplate;

    @GetMapping("send")
    public String send(String msg) {
        String str = msg.trim() + "\t" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        //投递消息
        System.out.println("生成者投递消息: " + str);
        amqpTemplate.convertAndSend(queueName, str);
        return str;
    }
}
